<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="60px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table :data="tableData" border>
      <el-table-column
        label="患者姓名"
        prop="patientName"
        align="center"
        min-width="100"
      />
      <el-table-column
        label="医生姓名"
        prop="doctorName"
        align="center"
        min-width="100"
      />
      <el-table-column
        label="处理状态"
        prop="process"
        align="center"
        min-width="100"
      />
      <el-table-column
        label="原因"
        prop="reason"
        align="center"
        min-width="150"
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
        min-width="160"
      />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-link type="primary" icon="el-icon-edit" @click="handleUpdate(row)"
            >修改</el-link
          >
          <el-link
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(row.id)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="患者" prop="patientId">
          <el-select v-model="form.patientId" placeholder="请选择患者">
            <el-option
              v-for="p in patientList"
              :key="p.id"
              :label="p.name"
              :value="p.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="医生" prop="doctorId">
          <el-select v-model="form.doctorId" placeholder="请选择医生">
            <el-option
              v-for="d in doctorList"
              :key="d.id"
              :label="d.name"
              :value="d.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="process">
          <el-radio-group v-model="form.process">
            <el-radio label="处理中">处理中</el-radio>
            <el-radio label="处理完成">处理完成</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入原因" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getEventList,
  addEvent,
  updateEvent,
  deleteEvent,
  getEvent,
} from "@/api/event";
import { getPatientsByDoctorId } from "@/api/patient";
import { listUser } from "@/api/system/user";

export default {
  data() {
    return {
      showSearch: true,
      tableData: [],
      total: 0,
      dialogVisible: false,
      title: "",
      form: {},
      queryParams: {
        name: undefined,
        pageIndex: 1,
        pageSize: 10,
      },
      rules: {
        patientId: [
          { required: true, message: "请选择患者", trigger: "change" },
        ],
        doctorId: [
          { required: true, message: "请选择医生", trigger: "change" },
        ],
        process: [
          { required: true, message: "请选择处理状态", trigger: "change" },
        ],
        reason: [{ required: true, message: "请输入原因", trigger: "blur" }],
      },
      patientList: [],
      doctorList: [],
    };
  },
  created() {
    this.getList();
    this.getPatients();
    this.getDoctors();
  },
  methods: {
    async getList() {
      const res = await getEventList(this.queryParams);
      this.tableData = res.items || [];
      this.total = res.total || 0;
    },
    async submit() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return;
        const action = this.form.id ? updateEvent : addEvent;
        const payload = { ...this.form };
        if (this.form.id) {
          await action(this.form.id, payload);
        } else {
          await action(payload);
        }
        this.$message.success("操作成功");
        this.dialogVisible = false;
        this.getList();
      });
    },
    async handleAdd() {
      this.form = {
        patientId: undefined,
        doctorId: undefined,
        process: "处理中",
        reason: "",
      };
      this.title = "新增记录";
      this.dialogVisible = true;
    },
    async handleUpdate(row) {
      this.form = row;
      this.title = "修改记录";
      this.dialogVisible = true;
    },
    async handleDelete(id) {
      await deleteEvent(id);
      this.$message.success("删除成功");
      this.getList();
    },
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = { name: undefined, pageIndex: 1, pageSize: 10 };
      this.getList();
    },
    async getPatients() {
      const res = await getPatientsByDoctorId({});
      this.patientList = res.data.list || [];
    },
    async getDoctors() {
      const res = await listUser({});
      this.doctorList = res.data.list || [];
    },
  },
};
</script>

<style scoped></style>
